package com.framework.utils;

import com.framework.utils.ali.AliOssUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;


import java.io.*;

/**
 * @author wq
 */
public class QRCodeUtils {
    /**
     * 生成二维码并上传
     *
     * @param url
     * @param width
     * @param height
     * @return
     */
    public static String generateQRAndUpload(String url, int width, int height,String token) {


        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        String result = null;
        try {
            BitMatrix bitMatrix = qrCodeWriter.encode(url, BarcodeFormat.QR_CODE, width, height);


            ByteArrayOutputStream os = new ByteArrayOutputStream();

            MatrixToImageWriter.writeToStream(bitMatrix, "jpg", os);

            ByteArrayInputStream inputStream = ConvertUtil.parse(os);

            result= AliOssUtils.uploadBytes(inputStream,"qrcode/"+token+".jpg");

        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


}
